<?php

include 'ConnectDB.php';
include 'graph.php';
define('FPDF_FONTPATH', './fpdf/font/');
require_once ("fpdf/fpdf.php");

$graphname = 'Revenue Mix Chart';
$connect = new ConnectDB();
$connect->connect();
class Brandmix{
function getRow() {
    $connect = new ConnectDB();
    $connect->connect();
    $query = "select distinct brand_c FROM sellout_products  where sell_out in (
	select id_c from sell_sellout_cstm where YEAR(date_s_c) = YEAR(NOW()) and id_c In (
	select  sell_out from sellout_products  ) )ORDER BY brand_c";
    $result = $connect->getdata($query);
    return $result;
}

function getAnswer($result) {
    $connect = new ConnectDB();
    $connect->connect();

    $query = "select  brand_c,total_price FROM sellout_products  where brand_c = '" . $result['brand_c'] . "' and sell_out in (
	select id_c from sell_sellout_cstm where YEAR(date_s_c) = YEAR(NOW()) and id_c In (
	select  sell_out from sellout_products  ) )";
    $answer = $connect->getdata($query);
    //$total = 0;
    foreach ($answer as $answer) {

        $total += $answer['total_price'];
        //หาผลรวมต่อปี
    }

    return $total;
    // echo "tOTAL =".$total."&nbsp&nbsp";
    // echo "AVG = ".$peravg;
    // echo "<br/><br/>";
}

}
class PDF extends FPDF {

    function LoadData($file) {
        //Read file lines
        $lines = file($file);
        $data = array();
        foreach ($lines as $line)
            $data[] = explode(';', chop($line));
        return $data;
    }

    function table($results, $all, $avg,$grandtotal) {
        $head = array('BRAND', 'YTD-OUT', 'REVENUE SHARE (YTD)');
        $width = array(50, 50, 50);
        for ($i = 0; $i < count($head); $i++)
            $this->Cell($width[$i], 5, iconv('UTF-8', 'TIS-620', $head[$i]), 1, 0, 'C');
        //หัววคอลัมท่ี2
        $this->Ln();
            $i=0;
   foreach ($results as $results){
       
        $this->Cell(50, 6, $results["brand_c"], 1, 0, 'C');
        $this->Cell(50, 6, $all[$i], 1, 0, 'C');
        $this->Cell(50, 6,number_format($avg[$i],2)."%", 1, 0, 'C');
        $i++;
        $this->Ln();
    }
         $this->Cell(50, 6, 'Grandtotal', 1, 0, 'C');
         $this->Cell(50, 6, $grandtotal, 1, 0, 'C');
         $this->Cell(50, 6, "100%", 1, 0, 'C');
    }

}

$pdf = new PDF;
$brandmix = new Brandmix;
$result = $brandmix->getRow();
foreach ($result as $result) {
    $total = $brandmix->getAnswer($result);//หาค่่าผลรวมยอดขายทั้งปี
    $grandtotal += $total;
}
$result = $brandmix->getRow();
$i = 0;
foreach ($result as $result) {
$all[$i] = $brandmix->getAnswer($result);//ค่าผลรวมยอดขาย
$avg[$i] = ($all[$i] * 100) / $grandtotal;//ผลเฉลี่ยของยอดขาย
$avg[$i] = number_format($avg[$i],2);
$avg[$i] = $avg[$i]."%";
$i++;
}
$result = $brandmix->getRow();

$graph = new Reportgraph;//สร้างกราฟ
$graph -> piegraph($graphname, $avg,$result);//ส่งค่าให้ฟังก์ชัน
$pdf->AddFont('tahoma', '', 'tahoma.php');
$pdf->SetFont('tahoma', '', 13);
$pdf->AddPage();
$pdf->Image('brandmix.png',40,50,120);
$pdf->table($result, $all, $avg,$grandtotal);
$pdf->Output('pdf/Brandmix.pdf');

?>


